using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame;

namespace MainGame.GamePlay
{
  using XNAColor = Microsoft.Xna.Framework.Color;

  public delegate void OnTurnOver();
  public delegate void OnMenuPause();

  /// <summary>
  /// Servizio di UI: lancia eventi per il click sui bottoni gestiti
  /// </summary>
  public interface IUIRenderer
  {
    event OnTurnOver OnTurnOver;
    event OnMenuPause OnMenuPause;
  }

  /// <summary>
  /// Disegna la UI del gioco e gestisce il click sul bottone Next Turn
  /// </summary>
  public class UIRenderer : Microsoft.Xna.Framework.DrawableGameComponent, IUIRenderer
  {
    float t = 0.0f;
    public UIRenderer(Game game)
      : base(game)
    {
      UpdateOrder = (int)ComponentOrders.UI;
      DrawOrder = (int)ComponentOrders.UI;

      game.Services.AddService(typeof(IUIRenderer), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(IUIRenderer));

      base.Dispose(disposing);
    }

    IBoardState board;
    public override void Initialize()
    {
      Game.GetInputManager().OnTap += new InputManager.Tap(UIRenderer_OnTap);
      board = Game.GetBoardManager();

      base.Initialize();
    }

    void UIRenderer_OnTap(Vector2 tap_position)
    {
      var p = tap_position.ToPoint(screen_transform);
      if (turn_done_button.Contains(p))
      {
        if (OnTurnOver != null)
          OnTurnOver();
      }

      if (menupause_button.Contains(p))
      {
          if (OnMenuPause != null)
              OnMenuPause();
      }
    }

    Rectangle turn_done_button = new Rectangle(730, 415, 50, 50);
    Rectangle menupause_button = new Rectangle(730, 5, 60, 30);
      Rectangle help_button = new Rectangle(10, 5, 60, 30);
    Matrix screen_transform;
    SpriteBatch sprite_batch;
    SpriteFont large_font;
    SpriteFont small_font;
    Texture2D white_pixel;
    protected override void LoadContent()
    {
      sprite_batch = Game.GetContext().SpriteBatch;
      screen_transform = Game.GetContext().ScreenTransform;
      large_font = Game.Content.Load<SpriteFont>("large_font");
      small_font = Game.Content.Load<SpriteFont>("small_font");
      white_pixel = Game.Content.Load<Texture2D>("white_pixel");
      base.LoadContent();
    }

    public override void Update(GameTime gameTime)
    {
      t += gameTime.dt();

      base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
      sprite_batch.Begin();

      // Disegna Punteggio Players 
      int y = 10;
      foreach (var player in board.Players)
      {          
          sprite_batch.DrawString(small_font, player.Name+" : "+player.Score.ToString(), new Vector2(600, y), XNAColor.Yellow);
          y += 30;
      }

      //Visualizza Turno
      sprite_batch.DrawString(large_font, "Turno: "+board.CurrentPlayerIndex.ToString(), new Vector2(150, 400), XNAColor.Yellow);

      //Visualizza Time
      sprite_batch.DrawString(large_font, "Time: "+t.ToString("0#.#"),
        new Vector2(300, 10), XNAColor.Yellow);

      //Visualizza Bottone Next Turn
      sprite_batch.Draw(white_pixel, turn_done_button, XNAColor.Yellow);
      sprite_batch.DrawString(small_font, "Raise\n Card",
        new Vector2(turn_done_button.X, turn_done_button.Y), XNAColor.Black);

      //Visualizza Bottone Menu/Pause
      sprite_batch.Draw(white_pixel, menupause_button, XNAColor.Yellow);
      sprite_batch.DrawString(small_font, "MENU", new Vector2(menupause_button.X, menupause_button.Y), XNAColor.Black);

      //Visualizza Bottone Help
      sprite_batch.Draw(white_pixel, help_button, XNAColor.Green);
      sprite_batch.DrawString(small_font, "HELP", new Vector2(help_button.X, help_button.Y), XNAColor.Black);

      sprite_batch.End();

      base.Draw(gameTime);
    }

    public event OnTurnOver OnTurnOver;
    public event OnMenuPause OnMenuPause;
  }
}
